package com.hello.sandbox.fake.frameworks;

import android.os.IBinder;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.List;
import com.hello.sandbox.app.BActivityThread;
import com.hello.sandbox.core.system.ServiceManager;
import com.hello.sandbox.core.system.location.IBLocationManagerService;
import com.hello.sandbox.entity.location.BCell;
import com.hello.sandbox.entity.location.BLocation;

/** Created by BlackBoxing on 3/8/22. */
public class BLocationManager extends BlackManager<IBLocationManagerService> {
  private static final BLocationManager sLocationManager = new BLocationManager();

  public static final int CLOSE_MODE = 0;
  public static final int GLOBAL_MODE = 1;
  public static final int OWN_MODE = 2;

  public static BLocationManager get() {
    return sLocationManager;
  }

  @Override
  protected String getServiceName() {
    return ServiceManager.LOCATION_MANAGER;
  }

  public static boolean isFakeLocationEnable() {
    return get().getPattern(BActivityThread.getUserId(), BActivityThread.getAppPackageName())
        != CLOSE_MODE;
  }

  public static void disableFakeLocation(int userId, String pkg) {
    get().setPattern(userId, pkg, CLOSE_MODE);
  }

  public void setPattern(int userId, String pkg, int pattern) {
    try {
      getService().setPattern(userId, pkg, pattern);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public int getPattern(int userId, String pkg) {
    try {
      return getService().getPattern(userId, pkg);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return CLOSE_MODE;
  }

  public void setCell(int userId, String pkg, BCell cell) {
    try {
      getService().setCell(userId, pkg, cell);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void setAllCell(int userId, String pkg, List<BCell> cells) {
    try {
      getService().setAllCell(userId, pkg, cells);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public List<BCell> getNeighboringCell(int userId, String pkg) {
    try {
      return getService().getNeighboringCell(userId, pkg);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<BCell> getGlobalNeighboringCell() {
    try {
      return getService().getGlobalNeighboringCell();
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return null;
  }

  public void setNeighboringCell(int userId, String pkg, List<BCell> cells) {
    try {
      getService().setNeighboringCell(userId, pkg, cells);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void setGlobalCell(BCell cell) {
    try {
      getService().setGlobalCell(cell);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void setGlobalAllCell(List<BCell> cells) {
    try {
      getService().setGlobalAllCell(cells);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void setGlobalNeighboringCell(List<BCell> cells) {
    try {
      getService().setGlobalNeighboringCell(cells);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public BCell getCell(int userId, String pkg) {
    try {
      return getService().getCell(userId, pkg);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<BCell> getAllCell(int userId, String pkg) {
    try {
      return getService().getAllCell(userId, pkg);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return new ArrayList<>();
  }

  public void setLocation(int userId, String pkg, BLocation location) {
    try {
      getService().setLocation(userId, pkg, location);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public BLocation getLocation(int userId, String pkg) {
    try {
      return getService().getLocation(userId, pkg);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return null;
  }

  public void setGlobalLocation(BLocation location) {
    try {
      getService().setGlobalLocation(location);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public BLocation getGlobalLocation() {
    try {
      return getService().getGlobalLocation();
    } catch (RemoteException e) {
      e.printStackTrace();
    }
    return null;
  }

  public void requestLocationUpdates(IBinder listener) {
    try {
      getService()
          .requestLocationUpdates(
              listener, BActivityThread.getAppPackageName(), BActivityThread.getUserId());
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  public void removeUpdates(IBinder listener) {
    try {
      getService().removeUpdates(listener);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }
}
